package com.hsy.linemoney.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.application.Application;
import com.hsy.linemoney.common.Config;
import com.hsy.linemoney.common.Consts;
import com.hsy.linemoney.common.GlobalLoadingPopup;
import com.hsy.linemoney.common.LoadingProgressDialog;

import java.math.BigDecimal;
import java.text.DecimalFormat;


/**
 * Created by hsy on 2016/3/23.
 */
public class CommonUtils {
    //TODO delete
    public static void saveProvince(Context context, String province) {
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().putString(Consts.KEY_USER_PROVINCE, province).commit();
    }

    //TODO delete
    public static String getProvince(Context context) {
        return context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).getString(Consts.KEY_USER_PROVINCE, "");
    }

    //TODO delete
    public static void saveCity(Context context, String city) {
        context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().putString(Consts.KEY_USER_CITY, city).commit();
    }

    //TODO delete
    public static String getCity(Context context) {
        return context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).getString(Consts.KEY_USER_CITY, "");
    }

    public static boolean getIsFirstInUse(Context context) {
        return context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).getBoolean(Consts.IS_APP_FIRST_IN_USE, true);
    }

    public static boolean setIsFirstInUse(Context context, boolean isFirstInUse) {
        return context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_APPEND).edit().putBoolean(Consts.IS_APP_FIRST_IN_USE, isFirstInUse).commit();
    }

    private static LoadingProgressDialog loadingProgressDialog;

    public static void showProgressDialog(Activity activity) {
        DPLogUtil.d("showProgressDialog");
//        Application.getInstance().showProgressDialog(activity);
        hideProgressDialog();
        if (null == loadingProgressDialog) {
            loadingProgressDialog = new LoadingProgressDialog(activity);
            loadingProgressDialog.setCancelable(false);
            loadingProgressDialog.setCanceledOnTouchOutside(false);
            loadingProgressDialog.show();
            loadingProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    loadingProgressDialog = null;
                }
            });
        }
    }

    public static void hideProgressDialog() {
        DPLogUtil.d("hideProgressDialog");
//        Application.getInstance().hideProgressDialog();
        if (null != loadingProgressDialog) {
            loadingProgressDialog.dismiss();
        }
        loadingProgressDialog = null;
    }

    public static AlertDialog showListDialog(Context context, String[] items, DialogInterface.OnClickListener listener) {
        return showListDialog(context, null, items, listener);
    }

    public static AlertDialog showListDialog(Context context, int titleRes, String[] items, DialogInterface.OnClickListener listener) {
        return showListDialog(context, context.getString(titleRes), items, listener);
    }

    public static AlertDialog showListDialog(Context context, String title, String[] items, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setItems(items, listener);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }

    public static String round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return String.valueOf(b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue());

    }

    public static String getMoney(String in) {
        if (TextUtils.isEmpty(in) || in.indexOf(".") < 0) {
            return in;
        }
        DecimalFormat df = new DecimalFormat("#.00");
        return df.format(Double.valueOf(in));
    }

    public static String getMoney(String in, String unit) {
        return getMoney(in) + unit;
    }

    public static String getRate(String in) {
        if (TextUtils.isEmpty(in) || in.indexOf(".") < 0) {
            return in;
        }
        DecimalFormat df = new DecimalFormat("#.0000");
        return df.format(Double.valueOf(in));
    }

    public static String getRate(String in, String unit) {
        return getRate(in) + unit;
    }
}
